package com.chengwei.ch.busdemo.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.content.ContextCompat;

import com.chengwei.ch.busdemo.R;


/**
 * 自定义标题栏功能的实现
 * <p/>
 *
 * @author zdk
 */
@SuppressLint({"Recycle", "CustomViewStyleable"})
public class TitleView extends ConstraintLayout {
    private Context mContext;
    private Integer allBackground;
    private Integer titleBackground;
    private String leftText;
    private Integer leftTextColor;
    private Integer leftIcon;
    private String titleText;
    private Integer titleColor;
    private String rightText;
    private Integer rightTextColor;
    private Integer rightIcon;

    private ConstraintLayout baseTopToolbar;
    private ImageButton IBtnTitleViewLeftBack, IBtnTitleViewRightMenu;
    private Button BtnTitleViewLeftBack, BtnTitleViewRightMenu;
    private TextView TvTitleViewTitle;

    public TitleView(Context context) {
        this(context, null);
    }

    public TitleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        View vTitle = LayoutInflater.from(context).inflate(R.layout.view_toolbar, this, true);
        baseTopToolbar = vTitle.findViewById(R.id.baseTopToolbar);
        IBtnTitleViewLeftBack = vTitle.findViewById(R.id.IBtnTitleViewLeftBack);
        IBtnTitleViewRightMenu = vTitle.findViewById(R.id.IBtnTitleViewRightMenu);
        BtnTitleViewLeftBack = vTitle.findViewById(R.id.BtnTitleViewLeftBack);
        BtnTitleViewRightMenu = vTitle.findViewById(R.id.BtnTitleViewRightMenu);
        TvTitleViewTitle = vTitle.findViewById(R.id.TvTitleViewTitle);
        TypedArray mAttrs = context.obtainStyledAttributes(attrs, R.styleable.TitleView);
        // 左侧按钮初始化
        leftText = mAttrs.getString(R.styleable.TitleView_leftText);
        leftTextColor = mAttrs.getColor(R.styleable.TitleView_leftTextColor, getResources().getColor(R.color.colorGray68));
        leftIcon = mAttrs.getResourceId(R.styleable.TitleView_leftIcon, 0);
        if (leftIcon != 0) {
            if (leftText != null) {
                //文字返回键按钮是否显示
                setLeftBtnVisible(true);
                //图片返回键按钮是否显示
                setLeftIbtnVisible(false);
                //自定义添加图片返回键
                setLeftBtnIcon(leftIcon);
                //自定义返回文字
                setLeftText(leftText);
                //自定义返回文字颜色
                setLeftColor(leftTextColor);
            } else {
                //图片返回键按钮是否显示
                setLeftIbtnVisible(true);
                //文字返回键按钮是否显示
                setLeftBtnVisible(false);
                //自定义添加图片返回键
                setLeftIbtnIcon(leftIcon);
            }
        } else {
            if (leftText != null) {
                //文字返回键按钮是否显示
                setLeftBtnVisible(true);
                //图片返回键按钮是否显示
                setLeftIbtnVisible(false);
                //自定义返回文字
                setLeftText(leftText);
                //自定义返回文字颜色
                setLeftColor(leftTextColor);
            } else {
                setLeftBtnVisible(false);
                setLeftIbtnVisible(false);
            }
        }
        // 设置返回标题背景色
        titleBackground = mAttrs.getColor(R.styleable.TitleView_titleBackground, getResources().getColor(R.color.colorWhite));
        setTitleBackground(titleBackground);
        // 标题设置
        titleText = mAttrs.getString(R.styleable.TitleView_titleText);
        if (titleText != null) {
            setTitleText(titleText);
        } else {
            setTitleText("标题");
        }
        titleColor = mAttrs.getColor(R.styleable.TitleView_titleTextColor, getResources().getColor(R.color.colorGray68));
        setTitleColor(titleColor);

        // 右侧按钮初始化
        rightText = mAttrs.getString(R.styleable.TitleView_rightText);
        rightTextColor = 0;
        rightTextColor = mAttrs.getColor(R.styleable.TitleView_rightTextColor, getResources().getColor(R.color.colorGray68));
        rightIcon = mAttrs.getResourceId(R.styleable.TitleView_rightIcon, 0);
        if (rightIcon != 0) {
            setRightBtnVisible(false);
            setRightIbtnVisible(true);
            setRightIcon(rightIcon);
        } else {
            if (rightText != null) {
                setRightBtnVisible(true);
                setRightIbtnVisible(false);
                setRightText(rightText);
                setRightColor(rightTextColor);
            } else {
                setRightBtnVisible(false);
                setRightIbtnVisible(false);
            }
        }
        mAttrs.recycle();
    }
    /** *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* */
    /** --------------------左侧按钮-------------------- */
    /**
     * =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
     */
    //左边返回键图片是否显示
    public void setLeftIbtnVisible(Boolean visible) {
        if (IBtnTitleViewLeftBack != null) {
            IBtnTitleViewLeftBack.setVisibility((visible) ? VISIBLE : GONE);
        }
    }

    //左边返回按钮是否显示
    public void setLeftBtnVisible(Boolean visible) {
        if (BtnTitleViewLeftBack != null) {
            BtnTitleViewLeftBack.setVisibility((visible) ? VISIBLE : GONE);
        }
    }

    //添加左边返回键图片
    public void setLeftIbtnIcon(@SuppressLint("SupportAnnotationUsage") @DrawableRes Integer icon) {
        this.leftIcon = icon;
        if (IBtnTitleViewLeftBack != null) {
            Drawable leftIconDrawable = ContextCompat.getDrawable(mContext, icon);
            IBtnTitleViewLeftBack.setImageDrawable(leftIconDrawable);
        }
    }

    //添加左边返回键btn图片
    public void setLeftBtnIcon(@SuppressLint("SupportAnnotationUsage") @DrawableRes Integer icon) {
        this.leftIcon = icon;
        if (BtnTitleViewLeftBack != null) {
            Drawable leftBtnDrawable = ContextCompat.getDrawable(mContext, icon);
            // 这一步必须要做,否则不会显示.
            assert leftBtnDrawable != null;
            leftBtnDrawable.setBounds(
                    0, 0, leftBtnDrawable.getMinimumWidth(),
                    leftBtnDrawable.getMinimumHeight()
            );
            BtnTitleViewLeftBack.setCompoundDrawables(
                    leftBtnDrawable,
                    null,
                    null,
                    null
            );
        }
    }

    //左边设置返回文字
    public void setLeftText(String text) {
        this.leftText = text;
        if (BtnTitleViewLeftBack != null) {
            BtnTitleViewLeftBack.setText(leftText);
        }
    }

    //左边设置返回文字
    public void setLeftColor(@ColorRes Integer color) {
        if (color == 0) {
            leftTextColor = R.color.colorGray68;
        } else {
            leftTextColor = color;
        }
        if (BtnTitleViewLeftBack != null) {
            BtnTitleViewLeftBack.setTextColor(leftTextColor);
        }
    }

    public void addTitleLeftOnClickListener(View.OnClickListener listener) {
        BtnTitleViewLeftBack.setOnClickListener(listener);
        IBtnTitleViewLeftBack.setOnClickListener(listener);
    }
    /** *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* */
    /** --------------------标题栏的设置-------------------- */
    /**
     * =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
     */
    public void setTitleText(String title) {
        this.titleText = title;
        if (TvTitleViewTitle != null) {
            TvTitleViewTitle.setText(titleText);
        }
    }

    public void setTitleColor(Integer titleColor) {
        if (titleColor == 0) {
            this.titleColor = R.color.colorGray68;
        } else {
            this.titleColor = titleColor;
        }
        if (TvTitleViewTitle != null) {
            TvTitleViewTitle.setTextColor(titleColor);
        }
    }
    /** *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* */
    /** --------------------标题栏背景色的设置-------------------- */
    /**
     * =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
     */
    public void setTitleBackground(Integer titleBackgroundColor) {
        if (titleBackgroundColor == 0) {
            this.titleBackground = R.color.colorWhite;
        } else {
            this.titleBackground = titleBackgroundColor;
        }
        if (baseTopToolbar != null) {
            baseTopToolbar.setBackgroundColor(titleBackgroundColor);
        }
    }
    /** *=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=* */
    /** --------------------右侧按钮-------------------- */
    /**
     * =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*
     */
    public void setRightBtnVisible(Boolean visible) {
        if (BtnTitleViewRightMenu != null) {
            BtnTitleViewRightMenu.setVisibility((visible) ? VISIBLE : GONE);
        }
    }

    public void setRightText(@StringRes Integer resId) {
        this.rightText = mContext.getString(resId);
        if (BtnTitleViewRightMenu != null) {
            BtnTitleViewRightMenu.setText(rightText);
        }
    }

    public void setRightText(CharSequence text) {
        this.rightText = text.toString();
        if (BtnTitleViewRightMenu != null) {
            BtnTitleViewRightMenu.setText(rightText);
        }
    }

    public void setRightColor(@ColorRes Integer color) {
        rightTextColor = (color == 0) ? R.color.colorGray68 : color;
        if (TvTitleViewTitle != null) {
            BtnTitleViewRightMenu.setTextColor(rightTextColor);
        }
    }

    public void setRightIbtnVisible(Boolean visible) {
        if (IBtnTitleViewRightMenu != null) {
            IBtnTitleViewRightMenu.setVisibility((visible) ? VISIBLE : GONE);
        }
    }

    public void setRightIcon(Integer icon) {
        rightIcon = icon;
        if (IBtnTitleViewRightMenu != null) {
            Drawable rightIconDrawable = ContextCompat.getDrawable(mContext, icon);
            IBtnTitleViewRightMenu.setImageDrawable(rightIconDrawable);
        }
    }

    public void addTitleRightOnClickListener(View.OnClickListener listener) {
        BtnTitleViewRightMenu.setOnClickListener(listener);
        IBtnTitleViewRightMenu.setOnClickListener(listener);
    }
}
